package com.bweab.controll.dormitorytbcontrol;

import com.bweab.service.api.DormitoryTbServiceInterface;
import com.bweab.service.impl.DormitoryTbServiceInterfaceImpl;
import com.bweab.util.JSONResult;
import com.bweab.util.JSONStats;
import com.bweab.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
@WebServlet("/addDormitoryInfo")
public class AddDormitoryInfor extends HttpServlet {

    //创建服务
    DormitoryTbServiceInterface dormitoryTbService =new DormitoryTbServiceInterfaceImpl();

    //创建返回结果
    JSONResult jsonResult=new JSONResult();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取参数
        String dormitoryName = req.getParameter("dormitoryName");
        int dormitoryFee = Integer.parseInt(req.getParameter("dormitoryFee"));


        //调用dao

        if (dormitoryName != null) {

            //调用dao

            int rows= dormitoryTbService.addDormitory(dormitoryName,dormitoryFee);

            if(rows>0){
                //创建成功
                jsonResult.setStatusCode(JSONStats.ADD_DORMITORY_SUCCESS.getStatusCode());
                jsonResult.setMessage(JSONStats.ADD_DORMITORY_SUCCESS.getMessage());

            }else {
                //创建失败
                jsonResult.setMessage(JSONStats.ADD_DORMITORY_ERROR.getMessage());
                jsonResult.setStatusCode(JSONStats.ADD_DORMITORY_ERROR.getStatusCode());
            }

        }else {
            //创建失败
            jsonResult.setMessage(JSONStats.ADD_DORMITORY_ERROR.getMessage());
            jsonResult.setStatusCode(JSONStats.ADD_DORMITORY_ERROR.getStatusCode());

        }

        WebUtil.writeJson(resp,jsonResult);
//        WebUtil.writeJson(resp,"测试");
    }
}
